<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" import="orm.*"
import="java.util.*" import="orm.Course" errorPage="errorPage.jsp" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% 	User tea  =(User) session.getAttribute("Teacher");  %>
<% 	String testId = (String)request.getAttribute("testId"); %>
<%	List knowledges = (List)request.getAttribute("knowledges");
	String knowledgesOpt = (String)request.getAttribute("knowledgesOpt");
	if ("".equals(knowledgesOpt)){
		knowledgesOpt = "<option value=''>当前课程无知识点</option>";
	}
%>
<%	String method = (String)request.getAttribute("method");
	String tIndex = "";
	if ("view".equals(method)){
		tIndex = (String)request.getAttribute("tid");
	}
	String display = "";
	Testtemplate template = null;
	if ("view".equals(method)){
		template = (Testtemplate)request.getAttribute("viewTemplate");
		if (template != null){
			Set tempknows = template.getTempknows();
			int i = 0; 
			for (Iterator it = tempknows.iterator(); it.hasNext(); i++){
				Tempknow t = (Tempknow)it.next();
				int index = knowledgesOpt.indexOf("value = '" + t.getKnowledge().getKid());
				if (index != -1){
					String kOpt = knowledgesOpt.substring(0, index) + "selected='selected'" + knowledgesOpt.substring(index);
					display += "<tr id='section_" + (i + 1) + "'><td><select name='knowledge_" + (i + 1) + "' id='knowledge_"
							 + (i + 1) + "' >" + kOpt + "</select></td><td><input type='text' value='" + t.getObjcnt() 
							 + "' name='number1_" + (i + 1) + "' id='number1_"
							 + (i + 1) + "' size='10' class='addTInput' onkeypress='return onlyNum(event);' maxlength='2'/></td>"
                             + "<td><input type='text' value='" + t.getObjscore() + "' name='score1_" + (i + 1) + "' id='score1_" 
                             + (i + 1) + "' size='10' class='addTInput' onkeypress='return onlyNum(event);' maxlength='2'/></td>"
                             + "<td><input type='text' value='" + t.getSubcnt() + "' name='number2_" + (i + 1) + "' id='number2_" 
                             + (i + 1) + "' size='10' class='addTInput' onkeypress='return onlyNum(event);' maxlength='2'/></td>"
                             + "<td><input type='text' value='" + t.getSubscore() + "' name='score2_" + (i + 1) + "' id='score2_" 
                             + (i + 1) + "' size='10' class='addTInput' onkeypress='return onlyNum(event);' maxlength='2'/></td>"
                             + "<td><input type='text' value='" + t.getObjmulcnt() + "' name='number3_" + (i + 1) + "' id='number3_" 
                             + (i + 1) + "' size='10' class='addTInput' onkeypress='return onlyNum(event);' maxlength='2'/></td>"
                             + "<td><input type='text' value='" + t.getObjmulscore() + "' name='score3_" + (i + 1) + "' id='score3_" 
                             + (i + 1) + "' size='10' class='addTInput' onkeypress='return onlyNum(event);' maxlength='2'/></td>";
                }
			}
		}
	}
	else if(!"add".equals(method)){
		response.sendRedirect("manageTemplates.do?action=startManagement");
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/tStyles.css" rel="stylesheet" type="text/css" />
<title>试卷模板添加</title>
<script type="text/javascript">
	theIndex = <% if("view".equals(method) && template != null){ Set tempknows = template.getTempknows();%><%=tempknows.size()%><%} else{%>1<%}%>;
	function addRow(){
		var id = "section_"+theIndex;
		<% if("view".equals(method)){%>
		if (theIndex >= <%=knowledges.size()%>){
			alert("已达到最大限制行数！");
	    	document.getElementById("addR").style.visibility = "hidden";
	    	return;
		}
		<%}%>
		theIndex++;
	    var secstr = document.getElementById(id).innerHTML;
	    var eNew = document.createElement("tr");
	    r = /_[0-9][0-9]?/g;
	    secstr = secstr.replace(r, "_" + theIndex);
	    eNew.setAttribute("id", "section_" + theIndex);
	    eNew.innerHTML += secstr;
	    document.getElementById('add').appendChild(eNew);
	    if (theIndex == 14 <% if (knowledges != null){%>|| theIndex >= <%=knowledges.size()%><%}%>){
	    	alert("已达到最大限制行数！");
	    	document.getElementById("addR").style.visibility = "hidden";
	    }
	}
	function commit(str, str2, str3){
		if (!checkInput()){
			alert("对不起，输入错误！");
			return false;
		}
		if (!checkKnowledge()){
			alert("对不起，每行的知识点必须不同！");
			return false;
		}
		if (str.indexOf("add") != -1){
			var opt = confirm("确认添加该模板？");
			if (opt == true){
				this.templateForm.action = "manageTemplates.do?action=commitTemplate&theIndex=" + theIndex + "&testId=" + str2;
				return this.templateForm.submit();
			}
			else{
				return false;
			}
		}
		else if(str.indexOf("view") != -1){
			var opt = confirm("确认修改该模板？");
			if (opt == true){
				this.templateForm.action = "manageTemplates.do?action=commitTemplate&theIndex=" + 
					theIndex + "&testId=" + str2 + "&tIndex=" + str3;
				return this.templateForm.submit();
			}
			else{
				return false;
			}
		}
		else {
			return false;
		}
	}
	function checkInput(){
		var i;
		for (i = 1; i <= theIndex; i++){
			var n1 = "number1_" + parseInt(i);
			var n2 = "number2_" + parseInt(i);
			var n3 = "number3_" + parseInt(i);
			var n4 = "score1_" + parseInt(i);
			var n5 = "score2_" + parseInt(i);
			var n6 = "score3_" + parseInt(i);
			if (document.getElementById(n1).value != "" && 
				document.getElementById(n2).value != "" && 
				document.getElementById(n3).value != "" &&
				document.getElementById(n4).value != "" && 
				document.getElementById(n5).value != "" && 
				document.getElementById(n6).value != ""){
				return true;
			}
		}
		if (i == (theIndex + 1)){
			return false;
		}
	}
	function checkKnowledge(){
		var k = new Array();
		var i;
		for (i = 1; i <= theIndex; i++){
			var s = "knowledge_" + parseInt(i);
			var l = k.length;
			while (l > 0){
				l--;
				if (k[l] == document.getElementById(s).value){
					return false;
				}
			}
			if (l == 0){
				k.push(document.getElementById(s).value);
			}
		}
		if (i == (theIndex + 1)){
			return true;
		}
	}
</script>
<script language="javascript" src="js/AjaxRequest.js">
</script>
<script type="text/javascript">
	function onlyNum(e){
		 var event = window.event || e;
    	 var key = document.all ? event.keyCode : event.which;
    	 return key >= 48 && key <= 57
	}
</script>
</head>

<body>
	<!-- CONTAINER START -->
<div id = "container">
<!-- HEADER START -->
        <div id = "top">
        	<div id = "navigation">
            	<html:link page="/teacher/teacher.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;首页</html:link>
                <html:link page="/teacher/notice.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;通知</html:link>
                <html:link action="manageQuestions.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;题库</html:link>
                <html:link action="manageKnowledges.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;知识点</html:link>
                <html:link action="manageTemplates.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;试卷模板</html:link>
                <html:link action="gradeManagement.do?action=startMana" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;成绩管理</html:link>
                <html:link page="/teacher/studentMana.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;学生管理</html:link>
            </div>
        </div>
        <!-- HEADER END -->
        
	<!-- LOGIN START -->
        <div id = "login">
        	<div id = "name">
        	<%=tea.getRealname() %>
            </div>
        	<html:link page="/teacher/teacherInfo.jsp" styleClass="viewInfo">·查询个人信息</html:link>
          	<p><html:link page="/teacher/teaModifyPwd.jsp" styleClass="modifyPwd">·修改密码</html:link></p>
            <p><html:link page="/teacher/letter.jsp" styleClass="letter">·站内信</html:link></p>
            <p><html:link forward="logout" styleClass="logout">·注销登录</html:link></p>
  </div>
        <!-- LOGIN END -->
        
        <!-- CLOCK START -->
  <div id = "clock">
        	<iframe src="index/clock.html" height="407" width="296" frameborder="0" scrolling="no"
            		marginheight="0" marginwidth="0" id="clockFrame" name="clockFrame"></iframe>
</div>
        <!-- CLOCK END -->
        
        <!-- MAIN START -->
      	<div id = "main">
        	<center>
        		<div id="template">
                	<br />
                	<% if (knowledges != null && knowledges.size() > 0){ %>
                	<a href="javascript:;" onclick="addRow();" style="font-size:24px;" id="addR" name="addR">增加一行</a>
                	<%} %>
                    <br />
                	<form method="post" name="templateForm" id="templateForm" 
                		  action="" onsubmit="return commit('<%=method %>', '<%=testId %>', '<%=tIndex %>');">
                		  
                    	<table border="1" style="text-align:center" id="add">
                    	  <tr>
                    	  	<td colspan="3" align="right" style="font-size: 20px;">模板名称：</td>
                    	  	<td colspan="4" align="left"><input type="text" name="tempName"
                    	  	<%if ("view".equals(method) && template != null){ %>value="<%=template.getTempName() %><%} %>" /></td>
                    	  </tr>
                          <tr style="font-size: 18px;">
                            <th width="109" scope="col">知识点</th>
                            <th width="119" scope="col">主观题题数</th>
                            <th width="125" scope="col">分数/题（主）</th>
                            <th width="119" scope="col">单选题题数</th>
                            <th width="125" scope="col">分数/题（单）</th>
                            <th width="119" scope="col">多选题题数</th>
                            <th width="125" scope="col">分数/题（多）</th>
                          </tr><% if ("view".equals(method)){ %><%=display %><%}else{ %>
                          <tr id="section_1">
                            <td><select name="knowledge_1" id="knowledge_1" >
                              <%=knowledgesOpt %>
                            </select></td>
                            <td><input type="text" name="number1_1" id="number1_1" size="10" class="addTInput" onkeypress="return onlyNum(event);" maxlength="2"/></td>
                            <td><input type="text" name="score1_1" id="score1_1" size="10" class="addTInput" onkeypress="return onlyNum(event);" maxlength="2"/></td>
                            <td><input type="text" name="number2_1" id="number2_1" size="10" class="addTInput" onkeypress="return onlyNum(event);" maxlength="2"/></td>
                            <td><input type="text" name="score2_1" id="score2_1" size="10" class="addTInput" onkeypress="return onlyNum(event);" maxlength="2"/></td>
                            <td><input type="text" name="number3_1" id="number3_1" size="10" class="addTInput" onkeypress="return onlyNum(event);" maxlength="2"/></td>
                            <td><input type="text" name="score3_1" id="score3_1" size="10" class="addTInput" onkeypress="return onlyNum(event);" maxlength="2"/></td>
                          </tr><%} %>
                        </table>
                        <% if (knowledges != null && knowledges.size() > 0){ %>
                        <input type="submit" value="确定提交" style="font-size:18px;" />
                        &nbsp;&nbsp;&nbsp;<%} %>
                        <input type="button" value="返回" style="font-size:18px;" onclick="window.location.href='manageTemplates.do?action=startManagement';"/>
                    </form>
                </div>
            </center>
   	  	</div>
		<!-- MAIN END -->
        
		<!-- FOOTER START -->
		<div id = "footer">
  	</div>
	<!-- FOOTER END -->
        
</div>
    <!-- CONTAINER END -->
</body>
</html>